﻿import struct,codecs,os
from win32com.client import Dispatch

xls=Dispatch('Excel.Application')
xls.Visible=0
xls.Workbooks.Add()

fp=open('strings.strl','rb')
tar=codecs.open('strings.strl'+'.txt','wb','utf_16')
fp.seek(0x28)
num=struct.unpack('i',fp.read(4))[0]
fp.seek(0x68)
baseoffset=struct.unpack('i',fp.read(4))[0]
for i in xrange(num):
    tar.write('No.%d\r\n'%i+u'－'*18+'\r\n')
    fp.seek(0x90+i*16+8)
    offset=struct.unpack('i',fp.read(4))[0]
    fp.seek(baseoffset+offset)
    string=[]
    while True:
        char=struct.unpack('H',fp.read(2))[0]
        if char==0:
            break
        elif char==10:
            string.append('\r\n')
        else:
            string.append(unichr(char))
    #print ''.join(string)
    tar.write(''.join(string)+'\r\n'+u'－'*18+'\r\n'*2)
    xls.Cells(1,1).Value=u'日文'
    xls.Cells(1,2).Value=u'译文'
    xls.Cells(1,3).Value=u'备注'
    xls.Cells(1,1).ColumnWidth=55
    xls.Cells(1,2).ColumnWidth=55
    xls.Cells(1,3).ColumnWidth=30
    xls.Cells(i+2,1).Value=''.join(string)

book=xls.Workbooks(1)
book.SaveAs(os.getcwd()+os.sep+'strings.xls')
xls.Quit()
fp.close()
tar.close()